package handler

import (
	"common/biz"
	"common/logs"
	"context"
	"core/models/entity"
	"core/repo"
	"core/service"
	"framework/msError"
)

type BaseHandler struct {
	userService *service.UserService
}

func (h *BaseHandler) Check(data *entity.User) *msError.Error {
	if data == nil {
		logs.Error("[Check]用户%d err:用户数据不存在 user=%v", data.Uid, data)
		return biz.InvalidUsers
	}
	if data.Status == 9 {
		logs.Error("[Check]用户%d err:账号已注销 user=%v", data.Uid, data)
		return biz.UserClenLoginError
	}
	if data.Status == -1 {
		logs.Error("[Check]用户%d err:账号已冻结 user=%v", data.Uid, data)
		return biz.BlockedAccount
	}
	if data.RealMsg.Name == "" {
		logs.Error("[Check]用户%d err:账号未实名 user=%v", data.Uid, data)
		return biz.RealError
	}
	//if data.WalletID <= 0 {
	//	logs.Error("[Check]用户%d err:账号未开通钱包 user=%v", data.Uid, data)
	//	return biz.WalletError
	//}
	return nil
}

func (h *BaseHandler) Login(user *entity.User, uid uint, sid uint64) *msError.Error {
	if user == nil {
		user, _ = h.userService.MongoGetUser(context.TODO(), uid)
		if user == nil {
			logs.Error("[Check]用户%d err:用户数据不存在 user=%v", uid, user)
			return biz.InvalidUsers
		}
	}
	if user.Sid != sid {
		logs.Error("[Check]用户%d err:socketID 不同已被挤下线 user=%v", uid, user)
		return biz.InvalidUsers
	}
	info := h.userService.GetConfInfo(context.TODO(), "is_maintain")
	if info != "0" {
		logs.Error("[Check]用户%d err:系统维护中 user=%v", uid, user)
		return biz.Maintain
	}
	return nil
}

func NewBaseHandler(r *repo.Manager) *BaseHandler {
	return &BaseHandler{
		userService: service.NewUserService(r),
	}
}
